<template>
  <div class="wrapper-list">
    <!-- 数据列表区 -->
    <div class="wrapper-table">
      <el-table
        v-loading="flagLoading"
        :data="tblDataList"
        :header-cell-style="{ 'text-align': 'center' }"
        row-key="id"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="编号" width="60" prop="id" />

        <el-table-column align="center" label="真实姓名" width="120" prop="realName" />

        <el-table-column align="center" label="用户名" prop="username" />

        <el-table-column align="center" label="所属科室" width="200" prop="departmentName" />

        <el-table-column align="center" label="手机" width="120" prop="mobilePhone" />

        <el-table-column align="center" label="性别" width="60" prop="gender" />

        <el-table-column align="center" label="注册时间" width="200" prop="dtCreate" />

        <el-table-column align="center" label="最后登录" width="200" prop="dtLastLogin" />

        <el-table-column align="center" label="登录次数" width="90" prop="loginNumber" />

        <el-table-column align="center" width="90" label="是否启用">
          <template #default="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#5B7BFA"
              inactive-color="#dadde5"
              @change="onEnabled(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column header-align="center" width="100" label="操作">
          <template #default="scope">
            <div class="wrapper-op">
              <a class="ibtn" title="重置密码" @click="onResetPassword(scope.row.id)">
                <el-icon color="#15364d"><Key /></el-icon>
              </a>
              <a class="ibtn ibtn-edit" title="修改" @click="onEdit(scope.row)">
                <el-icon><Edit /></el-icon>
              </a>
              <a class="ibtn ibtn-del" title="删除" @click="onDelete(scope.row.id)">
                <el-icon><Delete /></el-icon>
              </a>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { toastSuccess } from '@/utils/toast'
import { setSStorageObj } from '@/utils/web-storage'

import { ElMessageBox } from 'element-plus'

import router from '@/router'
import store from '@/store'
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'AdminListAllView',
  components: {},
  setup() {
    const tblDataList = ref([])
    const admin = ref({})
    const flagLoading = ref(true)

    // 从api获取所有管理员列表
    const listAll = () => {
      flagLoading.value = true
      store
        .dispatch('sys_admin/listAll', 'admin')
        .then((response) => {
          flagLoading.value = false
          tblDataList.value = response
        })
        .catch((error) => {
          flagLoading.value = false
          console.log('读取管理员列表数据出现错误：', error)
        })
    }
    // 改变启用状态
    const onEnabled = (val) => {
      const paramsJson = {
        AdminId: val.id,
        Action: 'Enabled',
      }
      store
        .dispatch('sys_admin/patchStatus', paramsJson)
        .then(() => {
          listAll()
        })
        .catch((error) => {
          console.log('修改启用状态出现错误：', error)
        })
    }
    const modifyPassword = (id) => {
      const paramsJson = {
        AdminId: id,
        Type: 'modify',
        PasswordOld: '',
        PasswordNew: '',
      }
      store
        .dispatch('sys_admin/modifyPassword', paramsJson)
        .then(() => {
          toastSuccess('密码修改成功')
          listAll()
        })
        .catch(() => {})
    }
    const onResetPassword = (id) => {
      ElMessageBox.confirm('确定要重置此管理员的密码吗？', '警告', {
        confirmButtonText: '确定重置',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const paramsJson = {
            AdminId: id,
            Type: 'reset-super',
          }
          store
            .dispatch('sys_admin/resetPasswordBySuper', paramsJson)
            .then(() => {
              toastSuccess('重置密码为 xzqwjw')
              listAll()
            })
            .catch(() => {})
        })
        .catch(() => {})
    }
    // 编辑管理员
    const onEdit = (row) => {
      setSStorageObj('ParamData', row)
      router.push({ path: 'edit' })
    }
    // 删除管理员
    const onDelete = (id) => {
      ElMessageBox.confirm('确定要删除此管理员吗？', '警告', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          store
            .dispatch('sys_admin/delById', id)
            .then(() => {
              toastSuccess('删除成功！')
              listAll()
            })
            .catch((error) => {
              console.log('删除管理员出现错误：', error)
            })
        })
        .catch(() => {})
    }

    onMounted(() => {
      listAll()
    })

    return {
      tblDataList,
      flagLoading,
      admin,
      listAll,
      onEnabled,
      modifyPassword,
      onResetPassword,
      onEdit,
      onDelete,
    }
  },
})
</script>

<style lang="scss" scoped></style>
